<?php

/*
 * Class designed to manage results
 */

class Result {

  public $id = null;

  public $home_team = null;
   
  public $home_score = null;
  
  public $away_team = null;
  
  public $away_score = null;
  
  public $event_id = null;


  public function __construct( $data=array() ) {
    if ( isset( $data['id'] ) ) $this->id = (int)$data['id'];
    if ( isset( $data['home_team'] ) ) $this->home_team = $data['home_team'];
    if ( isset( $data['home_score'] ) ) $this->home_score = $data['home_score'];
    if ( isset( $data['away_team'] ) ) $this->away_team = $data['away_team'];
    if ( isset( $data['away_score'] ) ) $this->away_score = $data['away_score'];
    if ( isset( $data['event_id'] ) ) $this->event_id = $data['event_id'];
  }


  public function storeFormValues( $params ) {
	  $this->__construct( $params );
  }


  public static function getById( $id ) {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT * FROM result WHERE id = :id";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":id", $id, PDO::PARAM_INT );
    $st->execute();
    $row = $st->fetch();
    $conn = null;
    if ( $row ) return new Result( $row );
  }


	public static function getByEvent( $event ) {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT * FROM result WHERE event_id = :event_id";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":event_id", $event, PDO::PARAM_INT );
    $st->execute();
    $list = array();

    while ( $row = $st->fetch() ) {
      $result = new Result( $row );
      $list[] = $result;
    }
    
    $conn = null;
    return ( array ( "results" => $list ) );
  }


  public static function getList( $numRows=1000000) {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM result
            LIMIT :numRows";

    $st = $conn->prepare( $sql );
    $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
    $st->execute();
    $list = array();

    while ( $row = $st->fetch() ) {
      $result = new Result( $row );
      $list[] = $result;
    }

    // Now get the total number of Match that matched the criteria
    $sql = "SELECT FOUND_ROWS() AS totalRows";
    $totalRows = $conn->query( $sql )->fetch();
    $conn = null;
    return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
  }


  public function insert() {

    // Does the Match object already have an ID?
    if ( !is_null( $this->id ) ) trigger_error ( "Result::insert(): Attempt to insert an Result object that already has its ID property set (to $this->id).", E_USER_ERROR );

    // Insert the Match
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $this->id = $conn->lastInsertId();
    $sql = "INSERT INTO result ( id, home_team, home_score, away_team, away_score, event_id ) VALUES ( :id, :home_team, :home_score, :away_team, :away_score, :event_id )";
    $st = $conn->prepare ( $sql );
    $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
    $st->bindValue( ":home_team", $this->home_team, PDO::PARAM_STR );
    $st->bindValue( ":home_score", $this->home_score, PDO::PARAM_INT );
    $st->bindValue( ":away_team", $this->away_team, PDO::PARAM_STR );
    $st->bindValue( ":away_score", $this->away_score, PDO::PARAM_INT );
    $st->bindValue( ":event_id", $this->event_id, PDO::PARAM_INT );
    $st->execute();
    $conn = null;
  }


  public function update() {
    if ( is_null( $this->id ) ) trigger_error ( "Result::update(): Attempt to update an Result object that does not have its ID property set.", E_USER_ERROR );

    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "UPDATE result SET home_team=:home_team, home_score=:home_score, away_team=:away_team, away_score=:away_score, event_id=:event_id WHERE id = :id";
    $st = $conn->prepare ( $sql );
    $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
    $st->bindValue( ":home_team", $this->home_team, PDO::PARAM_STR );
    $st->bindValue( ":home_score", $this->home_score, PDO::PARAM_INT );
    $st->bindValue( ":away_team", $this->away_team, PDO::PARAM_STR );
    $st->bindValue( ":away_score", $this->away_score, PDO::PARAM_INT );
    $st->bindValue( ":event_id", $this->event_id, PDO::PARAM_INT );
    $st->execute();
    $conn = null;
  }


  public function delete() {
    if ( is_null( $this->id ) ) trigger_error ( "Result::delete(): Attempt to delete an Result object that does not have an ID property.", E_USER_ERROR );

    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "DELETE FROM result WHERE id=:id LIMIT 1";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
    $st->execute();
    $conn = null;
  }

}
?>
